package com.ithuangyl.common.mvp.factory;

import com.ithuangyl.common.mvp.base.BaseMvpPresenter;
import com.ithuangyl.common.mvp.base.IBaseMvpView;

public class PresenterFactoryImpl<V extends IBaseMvpView, P extends BaseMvpPresenter<V>> implements IPresenterFactory<V, P> {
    /**
     * 需要创建的Presenter的类型
     */
    private final Class<P> mPresenterClass;

    public PresenterFactoryImpl(Class<P> presenterClass) {
        this.mPresenterClass = presenterClass;
    }

    public static <V extends IBaseMvpView, P extends BaseMvpPresenter<V>> PresenterFactoryImpl<V, P> createFactory(Class<?> viewClazz) {
        CreatePresenter annotation = viewClazz.getAnnotation(CreatePresenter.class);
        Class<P> aClass = null;
        if (null != annotation) {
            aClass = (Class<P>) annotation.value();
        }
        return aClass == null ? null : new PresenterFactoryImpl<V, P>(aClass);
    }

    @Override
    public P createPresenter() {
        try {
            return mPresenterClass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Presenter create failed!");
        }
    }
}

